package pe.com.certicom.moviles.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.sojo.interchange.json.JsonSerializer;

public class BaseController {
	
	protected static String[] FILTERED_ATTRS = {"~unique-id~","class"};

    /**
     * Construye el json de salida:
     * */
    protected void construirJson(Map<String,Object> respuesta, HttpServletResponse response) throws IOException{
    	Object jsonsalida = null;
        JsonSerializer serializer = new JsonSerializer();
        serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"), java.sql.Date.class);
		serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"), java.util.Date.class);
    	jsonsalida = serializer.serialize(respuesta,FILTERED_ATTRS);
		response.setContentType("text/plain;charset=ISO-8859-1");
		PrintWriter writer = response.getWriter(); 
		writer.write(jsonsalida.toString());
		writer.flush();
    }
    
    protected void construirJsonHTML(Map<String,Object> respuesta, HttpServletResponse response) throws IOException{
    	Object jsonsalida = null;
        JsonSerializer serializer = new JsonSerializer();
        serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"), java.sql.Date.class);
		serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"), java.util.Date.class);
    	jsonsalida = serializer.serialize(respuesta,FILTERED_ATTRS);
		response.setContentType("text/html;charset=ISO-8859-1");
		PrintWriter writer = response.getWriter();
		writer.write("<html><body><textarea>");
		writer.write(jsonsalida.toString());
		writer.write("</textarea></body></html>");
		writer.flush();
    }
    
    /**
     * Construye el json de salida:
     * */
    protected void construirJsonHora(Map<String,Object> respuesta, HttpServletResponse response) throws IOException{
    	Object jsonsalida = null;
        JsonSerializer serializer = new JsonSerializer();
        serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("HH:mm:ss"), java.sql.Date.class);
		serializer.getObjectUtil().addFormatterForType(new SimpleDateFormat("HH:mm:ss"), java.util.Date.class);
    	jsonsalida = serializer.serialize(respuesta,FILTERED_ATTRS);
		response.setContentType("text/plain;charset=ISO-8859-1");
		PrintWriter writer = response.getWriter(); 
		writer.write(jsonsalida.toString());
		writer.flush();
    }
	
}
